package com.javatom.notes.common.handler;

import com.javatom.notes.common.annotation.DistributedLock;
import com.javatom.notes.common.util.RedissonLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description:分布式锁注解解析器
 * @author: 曾维录
 * @time: 2019/7/29 16:24
 */
@Aspect
@Slf4j
public class DistributedLockHandler  {
    @Autowired
    RedissonLockUtil redissonLockUtil;

    /**
     * 执行RedisLock环绕通知
     * @param joinPoint joinPoint
     * @param distributedLock distributedLock
     * @return Object
     */
    @Around("@annotation(distributedLock)")
    public Object  around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) {
        log.info("[开始]执行RedisLock环绕通知,获取Redis分布式锁开始");
        //获取锁名称
        String lockName = distributedLock.value();
        //获取超时时间，默认10秒
        int leaseTime = distributedLock.leaseTime();
        redissonLockUtil.lock(lockName, leaseTime);
        Object proceed = null;
        try {
            log.info("获取Redis分布式锁[成功]，加锁完成，开始执行业务逻辑...");
            proceed = joinPoint.proceed();

        } catch (Throwable throwable) {
            log.error("获取Redis分布式锁[异常]，加锁失败", throwable);
            throwable.printStackTrace();
        } finally {
            //如果该线程还持有该锁，那么释放该锁。如果该线程不持有该锁，说明该线程的锁已到过期时间，自动释放锁
            if (redissonLockUtil.isHeldByCurrentThread(lockName)) {
                redissonLockUtil.unlock(lockName);
            }
        }
        log.info("释放Redis分布式锁[成功]，解锁完成，结束业务逻辑...");
        return proceed;
    }

}
